package com.canbot.u05.activity.customvoice.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.bean.CustomAudioData
import kotlinx.android.synthetic.main.custom_dialog_audio_view_item.view.*

/**
 * CustomDialogAudioAdapter
 *
 * @author  Liu.Heng
 * @date  2019/6/3
 */
internal class CustomDialogAudioAdapter(val context: Context, private val mList: List<CustomAudioData>, private val mOnItemClickListener: OnItemClickListener) : RecyclerView.Adapter<CustomDialogAudioAdapter.ViewHolder>() {

    private val mLayoutInflater = LayoutInflater.from(context)

    /**设定一个选中的标志位*/
    private var checked = -1

    interface OnItemClickListener {

        /***
         * 点击回调
         * @param view
         */
        fun onItemClick(view: View)
    }


    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
        val view = mLayoutInflater.inflate(R.layout.custom_dialog_audio_view_item, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val checkImage = holder.checkImage
        val musicText = holder.musicText
        val relativeLayout = holder.mRelativeLayout
        if (checked == position) {
            checkImage.visibility = View.VISIBLE
        } else {
            checkImage.visibility = View.GONE
        }
        musicText.text = mList[position].name
        relativeLayout.tag = position
        relativeLayout.setOnClickListener {
            mOnItemClickListener.onItemClick(it)
        }
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    fun setChecked(int: Int) {
        checked = int
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val checkImage: ImageView = view.check_image
        val musicText: TextView = view.music_text
        val mRelativeLayout: RelativeLayout = view.my_relativeLayout
    }
}